﻿// <copyright file="FederalElectionResultsQld2004FileDefinition.cs" company="QUT">
//     Copyright (c) Ninety Nine Percent. All rights reserved.
// </copyright>

using System;
using System.IO;
using FileHelpers;
using FileHelpers.DataLink;

namespace Qut.GenerateKml.Data.FileDefinitions
{
    /// <summary>
    /// Represents the file structure for the Federal Election Results-Qld-2004.xls data file.
    /// </summary>
    [DelimitedRecord("|")]
    public class FederalElectionResultsQld2004FileDefinition
    {
        /// <summary>
        /// Gets or sets the Seat
        /// </summary>
        public string Seat { get; set; }

        /// <summary>
        /// Gets or sets the Party
        /// </summary>
        public string Party { get; set; }

        /// <summary>
        /// Gets or sets the Percentage
        /// </summary>
        public string Percentage { get; set; }

        /// <summary>
        /// Gets or sets the Member of Parliament
        /// </summary>
        public string MP { get; set; }

        /// <summary>
        /// Gets or sets the Held Since
        /// </summary>
        public string HeldSince { get; set; }

        /// <summary>
        /// Gets or sets the Previously Held
        /// </summary>
        public string PreviouslyHeld { get; set; }

        /// <summary>
        /// Loads the specified data file from the file path.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="db">The data context.</param>
        public static void Load(string filePath, SociologicalDataClassesDataContext db)
        {
            var provider = new ExcelStorage(typeof(FederalElectionResultsQld2004FileDefinition), filePath, 2, 1);
            var results = provider.ExtractRecords() as FederalElectionResultsQld2004FileDefinition[];
            db.TruncateFederalElectionResultsQld2004();
            foreach (var item in results)
            {
                var record = new FederalElectionResultsQld2004();
                record.Seat = item.Seat;
                record.HeldSince = item.HeldSince;
                record.MP = item.MP;
                record.Party = item.Party;
                record.Percentage = item.Percentage;
                record.PreviouslyHeld = item.PreviouslyHeld;
                db.FederalElectionResultsQld2004s.InsertOnSubmit(record);
            }

            db.SubmitChanges();
        }
    }
}
